﻿using System.Collections.Generic;
using ZSoftInstagram.DataAccess.Abstractions;
using ZSoftInstagram.Domain;
using ZSoftInstagram.Domain.Board;
using ZSoftInstagram.Services.Abstraction;
using ZSoftInstagram.UI.ViewModels;
using System.Linq;
using System.Data.Entity;
using System.Threading.Tasks;

namespace ZSoftInstagram.Services.ImagePinProviders
{
    public class DefaultImagePinSuggestionProvider : IImagePinSuggestionProvider, IEntityService<ImagePin>
    {
        private IRepository<ImagePin> _repsitory;

        public DefaultImagePinSuggestionProvider(IRepository<ImagePin> repo)
        {
            this._repsitory = repo;
        }

        public IRepository<ImagePin> Repository
        {
            get
            {
                return this._repsitory;
            }
        }

        public Task<List<CategoryHomeModel>> ProviderByUserIdAsync(int userId)
        {
            return this.Repository.GetTypeRepository<Category>()
                            .Where(c => c.FollowingUsers.Any(u => u.Id == userId))
                                .Select(c => new CategoryHomeModel()
                                {
                                    Boards = c.ImagePinBoards.Select(imp => new PinBoardHomeModel()
                                    {
                                        Pins = imp.Pins.OrderByDescending(p => p.Date)
                                                .Where(p => !p.PinBoards.Any(a => a.UserId == userId))
                                                    .Select(p => new PinHomeViewModel()
                                                        {
                                                            Date = p.Date,
                                                            Description = p.Description,
                                                            Id = p.Id,
                                                            ImageAddress = p.Address.Path,
                                                            LikedUserIds = p.Likes.Select(l => l.User.Id)
                                                        })//,
                                        //CategoryIds = imp.Categories.Select(cat => cat.Id)
                                    })
                                }).ToListAsync();
        }
    }
}
